## This script: Local and national newspaper content using GLES Long-Term Media Agenda Analysis 
## ORIGINAL DATA IS AVAILABLE FROM GESIS: https://search.gesis.org/research_data/ZA5323
## GLES (2014). Long-Term Media Agenda Analysis (GLES). GESIS Data Archive, Cologne. ZA5323 Data file Version 2.0.0, https://doi.org/10.4232/1.11866.

library(tidyverse)
library(haven)

rm(list = ls())
options(scipen = 999) 


dat <- haven::read_dta("data/GLES_ZA5323_v2-0-0.dta") %>% 
  select(study, year, field, id, idm, tag, id2, nwords, starts_with('cat')) %>% 
  mutate(medium = case_when(
    idm == 1 ~ "Der Spiegel",
    idm == 2 ~ "Focus",
    idm == 3 ~ "Frankfurter Allgemeine Zeitung",
    idm == 4 ~ "Sueddeutsche Zeitung",
    idm == 5 ~ "BILD",
    idm == 6 ~ "Neue Westfaelische",
    idm == 7 ~ "Westdeutsche Allgemeine Zeitung",
    idm == 8 ~ "Hamburger Abendblatt",
    idm == 9 ~ "Stuttgarter Zeitung",
    idm == 10 ~ "Allgemeine Zeitung",
    idm == 11 ~ "Magdeburger Volksstimme",
    idm == 12 ~ "Weser Kurier",
    idm == 13 ~ "Berliner Zeitung",
    idm == 14 ~ "Schweriner Volkszeitung",
    idm == 15 ~ "Kieler Nachrichten",
    idm == 16 ~ "Hannoversche Allgemeine",
    idm == 17 ~ "Muenchner Merkur",
    idm == 18 ~ "Nuernberger Nachrichten",
    idm == 19 ~ "Frankfurter Neue Presse",
    TRUE ~ NA_character_ 
  )) %>% 
  mutate(local = case_when(
    idm %in% c(6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19) ~ 1,
    TRUE ~ 0 # Default case for all other idm values
  )) 



## Select relevant categories:

# Mentions of Klimaschutz & Naturschutz
cat_environ <- c('cat3510', 
                   'cat3520')
######
# Mentions of Migration
cat_immig <- c('cat3650')

######
# Mentions of Crime & Law&Order
cat_crime <- c('cat3310',
               'cat3320')

######
# Mentions of Infrastructure (urban planning/housing & traffic)
cat_infra <- c('cat3440',
               'cat3410')

######
# Mentions of School Education
cat_edu <- c('cat3910') 

##############################
## Create dummies of Mentioned - Yes/No, then run t-tests
##############################

######
# Mentions of Klimaschutz & Naturschutz
dat_environ <- dat %>% select(study, year, field, id, idm, medium, tag, id2, nwords, local,
                              all_of(cat_environ))
dat_environ <- dat_environ %>% mutate(environment = reduce(select(., starts_with("cat")), `+`) / nwords *100,
                                      environment2 = ifelse(environment > 0, 1, 0)) 
t_test <- broom::tidy(t.test(environment2 ~ local, data=dat_environ)) %>% mutate(outcome = "environment")


######
# Mentions of Migration
dat_immig <- dat %>% select(study, year, field, id, idm, medium, tag, id2, nwords, local,
                            all_of(cat_immig))
dat_immig <- dat_immig %>% mutate(immigration = reduce(select(., starts_with("cat")), `+`)/ nwords *100,
                                  immigration2 = ifelse(immigration > 0, 1, 0)) 

t_test <- bind_rows(t_test,broom::tidy(t.test(immigration2 ~ local, data=dat_immig)) %>% mutate(outcome = "migration"))


######
# Mentions of Crime & Law&Order
dat_crime <- dat %>% select(study, year, field, id, idm, medium, tag, id2, nwords, local,
                            all_of(cat_crime))
dat_crime <- dat_crime %>% mutate(crime = reduce(select(., starts_with("cat")), `+`)/ nwords *100,
                                  crime2 = ifelse(crime > 0, 1, 0)) 

t_test <- bind_rows(t_test,broom::tidy(t.test(crime2 ~ local, data=dat_crime)) %>% mutate(outcome = "laworder")) 

######
# Mentions of Stadtplanung/Wohnen & traffic
dat_infra <- dat %>% select(study, year, field, id, idm, medium, tag, id2, nwords, local,
                            all_of(cat_infra))
dat_infra <- dat_infra %>% mutate(infrastructure = reduce(select(., starts_with("cat")), `+`)/ nwords *100,
                                  infrastructure2 = ifelse(infrastructure > 0, 1, 0)) 

t_test <- bind_rows(t_test,broom::tidy(t.test(infrastructure2 ~ local, data=dat_infra)) %>% mutate(outcome = "infrastructure")) 

######
# Mentions of Education
dat_edu <- dat %>% select(study, year, field, id, idm, medium, tag, id2, nwords, local,
                          all_of(cat_edu))
dat_edu <- dat_edu %>% mutate(education = reduce(select(., starts_with("cat")), `+`)/ nwords *100,
                              education2 = ifelse(education > 0, 1, 0)) 

t_test <- bind_rows(t_test,broom::tidy(t.test(education2 ~ local, data=dat_edu)) %>% mutate(outcome = "education")) 


##
# Plot
##
t_test <- t_test %>% select(-estimate, -statistic, -parameter, -method, -alternative )
t_test <- t_test %>% pivot_longer(cols = starts_with("estimate"),
                                    names_to = "newspaper_type", values_to = "mean")
t_test <- t_test %>% mutate(newspaper_type = ifelse(newspaper_type == "estimate1", "National", "Local"))
t_test <- t_test  %>% mutate( outcome = ordered(as.factor(outcome),
                         levels = c("education", "infrastructure",
                            "environment", "migration", "laworder"),
                       labels = c("Education",  "Infrastructure",
                                   "Environment", "Migration", "Law & Order"))  )

p1 <- ggplot(t_test,aes(x=outcome,y=mean, fill=factor(newspaper_type)))+
  geom_bar(stat="identity",position="dodge")+
  xlab("Policy Field") +ylab("Share of Articles with Policy Content") +
  labs(fill = "Newspaper Type",
       labels= c("Local", "National")) +
  theme_bw() +
  theme(legend.position = 'bottom') +
  scale_fill_manual(values=c("black", "grey")) +
  coord_flip()


p1
